package org.zkdn.modules.wlpt.basicinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsideDeviceAll;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsideDeviceRsu;
import org.zkdn.modules.wlpt.basicinfo.enums.DeviceDtypeEnums;
import org.zkdn.modules.wlpt.basicinfo.mapper.RoadsideDeviceRsuMapper;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsideDeviceAllService;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsideDeviceRsuService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.zkdn.modules.wlpt.basicinfo.vo.RsuVO;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: roadside_device_rsu
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Service
public class RoadsideDeviceRsuServiceImpl extends ServiceImpl<RoadsideDeviceRsuMapper, RoadsideDeviceRsu> implements IRoadsideDeviceRsuService {

    @Autowired
    private IRoadsideDeviceAllService roadsideDeviceAllService;

    @Autowired
    private RoadsideDeviceRsuMapper roadsideDeviceRsuMapper;


    @Override
    public IPage<RsuVO> queryPageList(IPage<RsuVO> page, QueryWrapper queryWrapper) {
        return roadsideDeviceRsuMapper.queryPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveByValidate(RoadsideDeviceRsu roadsideDeviceRsu) {
        QueryWrapper<RoadsideDeviceRsu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", roadsideDeviceRsu.getDeviceId())
                .or().eq("serial_number", roadsideDeviceRsu.getSerialNumber());
        List<RoadsideDeviceRsu> list = roadsideDeviceRsuMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            RoadsideDeviceRsu mec = list.get(0);
            if(roadsideDeviceRsu.getDeviceId().equals(mec.getDeviceId())) {
                return Result.error("设备编号'" + roadsideDeviceRsu.getDeviceId() + "'已存在!" );
            }else if(roadsideDeviceRsu.getSerialNumber().equals(mec.getSerialNumber())) {
                return Result.error("设备序列号'" + roadsideDeviceRsu.getSerialNumber() + "'已存在!" );
            }
        }
        //先添加到全量设备表，并生成id
        RoadsideDeviceAll roadsideDeviceAll = new RoadsideDeviceAll();
        BeanUtils.copyProperties(roadsideDeviceRsu, roadsideDeviceAll);
        roadsideDeviceAll.setDtype(DeviceDtypeEnums.RSU.getDtype());
        roadsideDeviceAllService.save(roadsideDeviceAll);

        //保存到rsu表
        roadsideDeviceRsu.setId(roadsideDeviceAll.getId());
        roadsideDeviceRsuMapper.insert(roadsideDeviceRsu);
        return Result.OK("添加成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateByValidate(RoadsideDeviceRsu roadsideDeviceRsu) {
        QueryWrapper<RoadsideDeviceRsu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", roadsideDeviceRsu.getId())
                .and(wrapper -> wrapper.eq("device_id", roadsideDeviceRsu.getDeviceId())
                        .or().eq("serial_number", roadsideDeviceRsu.getSerialNumber()));
//        queryWrapper.eq("device_id", roadsideDeviceRsu.getDeviceId());
//        queryWrapper.ne("id", roadsideDeviceRsu.getId());
        List<RoadsideDeviceRsu> list = roadsideDeviceRsuMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            RoadsideDeviceRsu mec = list.get(0);
            if(roadsideDeviceRsu.getDeviceId().equals(mec.getDeviceId())) {
                return Result.error("设备编号'" + roadsideDeviceRsu.getDeviceId() + "'已存在!" );
            }else if(roadsideDeviceRsu.getSerialNumber().equals(mec.getSerialNumber())) {
                return Result.error("设备序列号'" + roadsideDeviceRsu.getSerialNumber() + "'已存在!" );
            }
        }

        //更新或保存全量设备表
        RoadsideDeviceAll roadsideDeviceAll = roadsideDeviceAllService.getById(roadsideDeviceRsu.getId());
        if(roadsideDeviceAll!=null) {
            BeanUtils.copyProperties(roadsideDeviceRsu, roadsideDeviceAll);
            roadsideDeviceAll.setDtype(DeviceDtypeEnums.CAMERA.getDtype());
            roadsideDeviceAllService.updateById(roadsideDeviceAll);
        }else {
            roadsideDeviceAll = new RoadsideDeviceAll();
            BeanUtils.copyProperties(roadsideDeviceRsu, roadsideDeviceAll);
            roadsideDeviceAll.setDtype(DeviceDtypeEnums.CAMERA.getDtype());
            roadsideDeviceAllService.save(roadsideDeviceAll);
        }

        roadsideDeviceRsuMapper.updateById(roadsideDeviceRsu);
        return Result.OK("修改成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(String id) {
        roadsideDeviceAllService.deleteById(id);
        roadsideDeviceRsuMapper.deleteById(id);
        return Result.OK("删除成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteByIds(String ids) {
        roadsideDeviceAllService.deleteByIds(ids);
        roadsideDeviceRsuMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }
}
